<?php
namespace Seller\Controller;
use Think\Controller;

class ReportController extends CommonController
{
    public $begin;
    public $end;
    public function _initialize()
    {
        $params = array();
        parent::_initialize($params);
        $this->assign("now_module" ,"report");
        $starttime = I('start_time');
        $endtime = I('end_time');
        if($starttime&&$endtime){
            $begin = $starttime;
            $end = $endtime;
        }else{
            $lastyear = date('Y-m-d H:i:s',strtotime("-3 month"));//30天前
            $begin = I('begin',$lastyear);
            $end =  I('end',date('Y-m-d H:i:s'));
        }
//        $this->assign('start_time',$begin);
//        $this->assign('end_time',$end);
        $this->begin = strtotime($begin);
        $this->end = strtotime($end);
        $this->assign('start_time',date('Y-m-d H:i:s',$this->begin));
        $this->assign('end_time',date('Y-m-d H:i:s',$this->end));
    }

    //订单统计
    public function order() {

        $st = strtotime(I("post.st"));
        $et = strtotime(I("post.et"));
        $where = "`time` >= '$st' AND `time` <= '$et'";
        if (!$st && !$et) {
            $where = "1 = 1";
        }
        $shop_id = session('shop_id');

        //已完成金额和单量
        $complete_amt = M("order")->where(array('shop_id' => $shop_id, 'status' => 3))->where($where)->sum("price");
        $compete_num = M("order")->where(array('shop_id' => $shop_id, 'status' => 3))->where($where)->count();
        $complete_amt = empty($complete_amt) ? 0 : $complete_amt;

        //已取消
        $cancel_amt = M("order")->where(array('shop_id' => $shop_id, 'status' => -1))->where($where)->sum("price");
        $cancel_num = M("order")->where(array('shop_id' => $shop_id, 'status' => -1))->where($where)->count();
        $cancel_amt = empty($cancel_amt) ? 0 : $cancel_amt;

        //进行中
        $amt = M("order")->where(array('shop_id' => $shop_id, 'status' => 0))->where($where)->sum("price");
        $count = M("order")->where(array('shop_id' => $shop_id, 'status' => 0))->where($where)->count();
        $amt = empty($amt) ? 0 : $amt;

        $this->assign('complete_amt', $complete_amt);
        $this->assign('compete_num', $compete_num);
        $this->assign('cancel_amt', $cancel_amt);
        $this->assign('cancel_num', $cancel_num);
        $this->assign('amt', $amt);
        $this->assign('count', $count);
        $this->display();
    }

    //报表统计
    public function sales_profile(){
            $Model = new \Think\Model();
            //今天九点开始
            $now = strtotime(date('Y-m-d'));
            $shop_id = session("shop_id");
            $today['today_amount'] = M('order')->where("time>$now AND status = 3 AND shop_id = $shop_id")->sum('price');//今日销售总额
            $today['today_order'] = M('order')->where("time>$now AND status = 3 AND shop_id = $shop_id")->count();//今日订单数
            $today['cancel_order'] = M('order')->where("time>$now AND status = -1 AND shop_id = $shop_id")->count();//今日取消订单
            $today['sign'] = round($today['today_amount']/$today['today_order'],2);
            $this->assign('today',$today);
            $sql = "SELECT COUNT(*) as tnum,sum(price) as amount, FROM_UNIXTIME(time,'%Y-%m-%d') as gap from  yd_order ";
            $sql .= " where time>$this->begin and time<$this->end AND status=3 AND shop_id = $shop_id  group by gap ";
            $res = $Model->query($sql);//订单数,交易额

//            一年的订单数交易额
            $tnum = 0;
            $tamount = 0;
            foreach ($res as $val){
                $arr[$val['gap']] = $val['tnum'];
                $brr[$val['gap']] = $val['amount'];
                $tnum += $val['tnum'];
                $tamount += $val['amount'];
            }

            for($i=$this->begin;$i<=$this->end;$i=$i+24*3600){
                $tmp_num = empty($arr[date('Y-m-d',$i)]) ? 0 : $arr[date('Y-m-d',$i)];
                $tmp_amount = empty($brr[date('Y-m-d',$i)]) ? 0 : $brr[date('Y-m-d',$i)];
                $tmp_sign = empty($tmp_num) ? 0 : round($tmp_amount/$tmp_num,2);
                $order_arr[] = $tmp_num;
                $amount_arr[] = $tmp_amount;
                $sign_arr[] = $tmp_sign;
                $date = date('Y-m-d',$i);
                $list[] = array('day'=>$date,
                          'order_num'=>$tmp_num,
                             'amount'=>$tmp_amount,
                               'sign'=>$tmp_sign,
                                'end'=>date('Y-m-d',$i+24*60*60));
                $day[] = $date;
            }
            //反键
            rsort($list);
            $res = array_page($list);
            $show = $res['page']->show();
            $this->assign('page',$show);
            $this->assign('list',$res['arr']);
            $result = array('order'=>$order_arr,'amount'=>$amount_arr,'sign'=>$sign_arr,'time'=>$day);
            $this->assign('result',json_encode($result));
            $this->display();
        }

        //商品排行
    public function hot_ranking()
    {
        $Model = new \Admin\Model\OrderGoodViewModel();
        //分类列表
        $shop_id = session("shop_id");
        $categories = M('category')->where(array('status' => 1))->order('sort ASC')->select();
        $category = sortCate($categories);
        //品牌列表
        $brand = M('brand')->where(array('status' => 1, 'shop_id' => $shop_id))->order('id DESC')->select();
        if (IS_AJAX) {
            $cat_id = I("post.cat_id");
            $brand_id = I("post.brand_id");
            $start_time = I("post.start_time");
            $end_time = I("post.end_time");
            $orderby = I("post.orderby");
            if (!$start_time) {
                $start_time = $this->begin;
            } else {
                $start_time = strtotime($start_time);
            }
            if (!$end_time) {
                $end_time = $this->end;
            } else {
                $end_time = strtotime($end_time);
            }
            if (!$orderby) {
                $orderby = "sumnum DESC";
            }
//                实例化模型grouby
            $res = $Model->where("$start_time < time AND $end_time > time AND shop_id = $shop_id")
                ->field('good_id,time,sum(goods_price) as sumprice,sum(num) as sumnum')
                ->group('good_id')->limit(50)->order($orderby)->select();
            if (!empty($cat_id)) {
                $where['cate'] = $cat_id;
            }
            if (!empty($brand_id)) {
                $where['brand_id'] = $brand_id;
            }
            $where['shop_id'] = $shop_id;
            //筛选是否符合条件
            foreach ($res as $key => $value) {
                $where['id'] = $value['good_id'];
                $result = M('good')->where($where)->find();
                if (!$result) {
                    unset($res[$key]);
                } else {
                    $res[$key]['good_name'] = $result['g_name'];
                    $res[$key]['good_sn'] = $result['good_sn'];
                }
            }
            //重新进行排序
            $res = resort($res);
            $this->assign('res', $res);
            $this->display('hot_query');
        } else {
            $res = $Model->where("$this->begin < time AND $this->end > time  AND shop_id = $shop_id")
                ->field('good_id,time,sum(goods_price) as sumprice,sum(num) as sumnum')
                ->group('good_id')->limit(50)->order('sumnum DESC')->select();
            foreach ($res as $key => $value) {
                $good_info = M('good')->where(array('id' => $value['good_id'], 'shop_id' => $shop_id))->find();
                $value['good_sn'] = $good_info['good_sn'];
                $value['good_name'] = $good_info['g_name'];
                $res[$key] = $value;
            }
            $this->assign('categories', $category);
            $this->assign('brands', $brand);
            $this->assign('res', $res);
            $this->display();
        }
    }

        //代理排行
    public function agent_ranking(){
            $Model = new \Admin\Model\OrderViewModel();
            $shop_id = session("shop_id");
            if(IS_AJAX){
                $start_time = I("post.start_time");
                $end_time = I("post.end_time");
                $orderby = I("post.orderby");
                if(!$start_time){
                    $start_time = $this->begin;
                }else{
                    $start_time = strtotime($start_time);
                }
                if(!$end_time){
                    $end_time = $this->end;
                }else{
                    $end_time = strtotime($end_time);
                }
                if(!$orderby){
                    $orderby = "sumnum DESC";
                }
                $res=$Model->where("$start_time < time AND $end_time > time AND agent.shop_id = $shop_id")
                           ->field('count(*) as sumnum,agent_id,time,agent_name,sum(price) as sumprice,')
                           ->group('agent_id')
                           ->limit(50)
                           ->order($orderby)->select();
                $this->assign('res',$res);
                $this->display('agent_query');
            }else{
                $res=$Model
                    ->where("$this->begin < time AND $this->end > time AND agent.shop_id = $shop_id")
                    ->field('count(*) as sumnum,agent_id,time,agent_name,sum(price) as sumprice,')
                    ->group('agent_id')
                    ->limit(50)
                    ->order('sumnum DESC')->select();
                $this->assign('res',$res);
                $this->display();
            }
        }
}